package com.commerce.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.commerce.common.entity.Addresses;
import com.commerce.user.mapper.AddressMapper;
import com.commerce.user.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 地址服务实现
 */
@Slf4j
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Addresses> implements AddressService {

    @Override
    public List<Addresses> getUserAddresses(Long userId) {
        LambdaQueryWrapper<Addresses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Addresses::getUserId, userId)
                   .orderByDesc(Addresses::getIsDefault)
                   .orderByDesc(Addresses::getCreateTime);
        
        return list(queryWrapper);
    }

    @Override
    @Transactional
    public boolean addAddress(Addresses address) {
        address.setCreateTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        
        // 如果设置为默认地址，需要先取消其他默认地址
        if (address.getIsDefault() == 1) {
            clearDefaultAddress(address.getUserId());
        }
        
        return save(address);
    }

    @Override
    @Transactional
    public boolean updateAddress(Addresses address) {
        address.setUpdateTime(LocalDateTime.now());
        
        // 如果设置为默认地址，需要先取消其他默认地址
        if (address.getIsDefault() == 1) {
            clearDefaultAddress(address.getUserId());
        }
        
        return updateById(address);
    }

    @Override
    @Transactional
    public boolean setDefaultAddress(Long addressId, Long userId) {
        // 先取消所有默认地址
        clearDefaultAddress(userId);
        
        // 设置指定地址为默认
        LambdaUpdateWrapper<Addresses> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Addresses::getId, addressId)
                    .eq(Addresses::getUserId, userId)
                    .set(Addresses::getIsDefault, 1)
                    .set(Addresses::getUpdateTime, LocalDateTime.now());
        
        return update(updateWrapper);
    }

    @Override
    public Addresses getUserDefaultAddress(Long userId) {
        LambdaQueryWrapper<Addresses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Addresses::getUserId, userId)
                   .eq(Addresses::getIsDefault, 1);
        
        return getOne(queryWrapper);
    }

    /**
     * 清除用户的所有默认地址
     */
    private void clearDefaultAddress(Long userId) {
        LambdaUpdateWrapper<Addresses> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Addresses::getUserId, userId)
                    .set(Addresses::getIsDefault, 0)
                    .set(Addresses::getUpdateTime, LocalDateTime.now());
        
        update(updateWrapper);
    }
}
